<?php
namespace hclient\formatters;
use hclient\base\Response;

/**
 * Response http 协议解析
 *<B>说明：</B>
 *<pre>
 * 略
 *</pre>
 */
class HttpParser implements ParserInterface
{
    /**
     * 解析请求返回数据 json 格式
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     * @param Response $response
     * @return mixed
     */
    public function parse(Response $response):void
    {
        $responseRawContent = explode(str_repeat("\r\n",2), $response->getContent(), 2);
        $headers = isset($responseRawContent[0]) ? $responseRawContent[0] : [];
        $content = isset($responseRawContent[1]) ? $responseRawContent[1] : null;

        $response->setContent($content);
        if (!empty($headers)) {
            $header_list = explode("\r\n",$headers);
            $response->setHeaders($header_list);
        }
    }
}
